package com.team.ai.service;

import com.team.ai.entity.Collect;
import com.team.ai.entity.ResultEntity;
import com.team.ai.mapper.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CollectionService {
    @Resource
    CollectionMapper collectionMapper;
    @Resource
    VideoMapper videoMapper;
    @Resource
    StuVideoMapper stuVideoMapper;
    @Resource
    BookMapper bookMapper;
    public String insert(int userId,int vbId,int idx){
        try{
            if (idx==1){  //首页视频
                return ResultEntity.success(collectionMapper.insert(userId, vbId,idx)) ;
            }else if (idx==2){ // 视频
                return ResultEntity.success(collectionMapper.insert(userId, vbId,idx)) ;
            }else{ // 文章
                return ResultEntity.success(collectionMapper.insert(userId, vbId,idx)) ;
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }
    public String delete(int userId,int vbId,int idx){
        try{
            if (idx==1){  //首页视频
                return ResultEntity.success(collectionMapper.delete(userId, vbId,idx)) ;
            }else if (idx==2){ // 视频
                return ResultEntity.success(collectionMapper.delete(userId, vbId,idx)) ;
            }else{ // 文章
                return ResultEntity.success(collectionMapper.delete(userId, vbId,idx)) ;
            }
        }catch (Exception e){
            e.printStackTrace();

            return  ResultEntity.fail(-10005);
        }
    }


    public String selectList(int userId,int idx){
        try{
            List<Collect> list = collectionMapper.selectList(userId,idx);
            List<Object> ans = new ArrayList<>();
            for (Collect i:list) {
                int vbid = i.getVbid();
                if (idx == 1){
                    ans.add(videoMapper.selectDetail(vbid));
                } else if (idx ==2) {
                    ans.add(stuVideoMapper.selectDetail(vbid));
                }else{
                    ans.add(bookMapper.selectDetail(vbid));
                }
            }
            return ResultEntity.success(ans) ;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }
}
